# -*- coding: utf-8 -*-
from PySide2 import QtWidgets
import maya.cmds as mc
import os
import DH_PySideUidata

#filepath = str(os.path.split(os.path.realpath(__file__))[0])
form_class, base_class = DH_PySideUidata.loadUiType('D:/dango/DH_IKFKSW/DH_IKFK_Generator.ui')


class DH_IKFKSW_Generator_Tool(base_class, form_class):

    def __init__(self):
        self.window_name = 'DH_IKFKSW_Generator_Tool'
        if mc.window(self.window_name, exists=True):
            mc.deleteUI(self.window_name)
        super(DH_IKFKSW_Generator_Tool, self).__init__(parent=DH_PySideUidata.getMayaWindow())
        self.setupUi(self)
        desktop = QtWidgets.QApplication.desktop().availableGeometry()
        size = self.geometry()
        self.move((desktop.width()-size.width())/2, (desktop.height()-size.height())/2)
        
        self.connect_button_func() 
        self.apply.clicked.connect(self.write_into_config) 
    

    
    def connect_button_func(self):#�������Ӱ�ť�ͺ���
        
        self.pushButton_H0.clicked.connect(lambda:self.input_lineEdit(self.lineEdit_H0))
        self.pushButton_H1.clicked.connect(lambda:self.input_lineEdit(self.lineEdit_H1))
        self.pushButton_H2.clicked.connect(lambda:self.input_lineEdit(self.lineEdit_H2))
        self.pushButton_H3.clicked.connect(lambda:self.input_lineEdit(self.lineEdit_H3))
        self.pushButton_H4.clicked.connect(lambda:self.input_lineEdit(self.lineEdit_H4))
        self.pushButton_H5.clicked.connect(lambda:self.input_lineEdit(self.lineEdit_H5))
        self.pushButton_H6.clicked.connect(lambda:self.input_lineEdit(self.lineEdit_H6))
        self.pushButton_H7.clicked.connect(lambda:self.input_lineEdit(self.lineEdit_H7))
        self.pushButton_H8.clicked.connect(lambda:self.input_lineEdit(self.lineEdit_H8))
        self.pushButton_H9.clicked.connect(lambda:self.input_lineEdit(self.lineEdit_H9))
        self.pushButton_H10.clicked.connect(lambda:self.input_lineEdit(self.lineEdit_H10))
        self.pushButton_H11.clicked.connect(lambda:self.input_lineEdit(self.lineEdit_H11))
        self.pushButton_H12.clicked.connect(lambda:self.input_lineEdit(self.lineEdit_H12))
        self.pushButton_H13.clicked.connect(lambda:self.input_lineEdit(self.lineEdit_H13))
        self.pushButton_H14.clicked.connect(lambda:self.input_lineEdit(self.lineEdit_H14))
        self.pushButton_H15.clicked.connect(lambda:self.input_lineEdit(self.lineEdit_H15))
        self.pushButton_HSW.clicked.connect(lambda:self.input_lineEdit(self.lineEdit_HSW))
        self.pushButton_FSW.clicked.connect(lambda:self.input_lineEdit(self.lineEdit_FSW))
        self.pushButton_readcfg.clicked.connect(self.readcfg)
    
    def input_lineEdit(self,whitch_lineEdit):#����������ѡ����������ƣ����ѡ��������ֻ�����һ��ѡ�������
        select_obj = mc.ls(sl = 1)
        if select_obj:
            whitch_lineEdit.setText(select_obj[0])
        else:
            self.erroroutput(u"����ѡ�����壡")
                    
    def erroroutput(self,errorContent):# �����������
        mc.confirmDialog(title=u'��ʾ', message=errorContent, button=['ok'])
        
        
    def readline(self):#��ȡ�û��������Ϣ
        return [self.lineEdit_PN.text(),\
        self.lineEdit_H0.text(),\
        self.lineEdit_H1.text(),\
        self.lineEdit_H2.text(),\
        self.lineEdit_H3.text(),\
        self.lineEdit_H4.text(),\
        self.lineEdit_H5.text(),\
        self.lineEdit_H6.text(),\
        self.lineEdit_H7.text(),\
        self.lineEdit_H8.text(),\
        self.lineEdit_H9.text(),\
        self.lineEdit_H10.text(),\
        self.lineEdit_H11.text(),\
        self.lineEdit_H12.text(),\
        self.lineEdit_H13.text(),\
        self.lineEdit_H14.text(),\
        self.lineEdit_H15.text(),\
        self.lineEdit_HL.text(),\
        self.lineEdit_HR.text(),\
        self.lineEdit_FL.text(),\
        self.lineEdit_FR.text(),\
        self.lineEdit_HIK.text(),\
        self.lineEdit_HFK.text(),\
        self.lineEdit_FIK.text(),\
        self.lineEdit_FFK.text()]
        
    def readcfg(self):
        projname = self.lineEdit_PN.text().encode('utf-8')
        if not projname:
            self.erroroutput(u"����������Ŀ�����ٶ������á�")
            return
        script_dir = mc.internalVar(usd=1)
        file_path = script_dir+"DH_IKFKSW/projects/"+projname+".swfile"#�����ļ���C:\Users\%username%\Documents\maya\2017\scripts\DH_IKFKSW\projects
        config_content = []
        if os.path.isfile(file_path):
            config_file = open(file_path, 'r')
            config_content = config_file.read().split("%%")
            config_file.close()
        if config_content:
            self.lineEdit_PN.setText(config_content[0])
            self.lineEdit_H0.setText(config_content[1])
            self.lineEdit_H1.setText(config_content[2])
            self.lineEdit_H2.setText(config_content[3])
            self.lineEdit_H3.setText(config_content[4])
            self.lineEdit_H4.setText(config_content[5])
            self.lineEdit_H5.setText(config_content[6])
            self.lineEdit_H6.setText(config_content[7])
            self.lineEdit_H7.setText(config_content[8])
            self.lineEdit_H8.setText(config_content[9])
            self.lineEdit_H9.setText(config_content[10])
            self.lineEdit_H10.setText(config_content[11])
            self.lineEdit_H11.setText(config_content[12])
            self.lineEdit_H12.setText(config_content[13])
            self.lineEdit_H13.setText(config_content[14])
            self.lineEdit_H14.setText(config_content[15])
            self.lineEdit_H15.setText(config_content[16])
            self.lineEdit_HL.setText(config_content[17])
            self.lineEdit_HR.setText(config_content[18])
            self.lineEdit_FL.setText(config_content[19])
            self.lineEdit_FR.setText(config_content[20])
            self.lineEdit_HIK.setText(config_content[21])
            self.lineEdit_HFK.setText(config_content[22])
            self.lineEdit_FIK.setText(config_content[23])
            self.lineEdit_FFK.setText(config_content[24])
        return True
        
        
        
    def create_newnode(self,IK_con,pole_con,FK_shoulder,FK_wrist):#�����µ�׷�ٽڵ�
        if mc.objExists(IK_con+"_for_IK_Tracking"):
            if mc.objExists(pole_con+"_for_IK_Tracking"):
                mc.delete(pole_con+"_for_IK_Tracking")
            mc.delete(IK_con+"_for_IK_Tracking")
        IKctrl_newnodeName = mc.duplicate(IK_con,n=IK_con+"_for_IK_Tracking",rc=1)[0].encode('utf-8')#��������ɵ�׷�ٽڵ������
        pole_newnodeName = mc.duplicate(pole_con,n=pole_con+"_for_IK_Tracking",rc=1)[0].encode('utf-8')
        if mc.objExists(IKctrl_newnodeName) and mc.objExists(pole_newnodeName):
            mc.delete(mc.listRelatives(IKctrl_newnodeName,c=1),IKctrl_newnodeName+"Shape",mc.listRelatives(pole_newnodeName,c=1),pole_newnodeName+"Shape")#ɾ���ӽڵ��shape�ڵ�
            mc.parent(pole_newnodeName,FK_shoulder)#p��FK������
            mc.parent(IKctrl_newnodeName,FK_wrist)
        return IKctrl_newnodeName,pole_newnodeName
    
    def create_newnode_doit(self):
        H_IK_con = self.lineEdit_H4.text()
        H_pole_con = self.lineEdit_H3.text()
        H_FK_shoulder = self.lineEdit_H5.text()
        H_FK_wrist = self.lineEdit_H7.text()
        H_left_string = self.lineEdit_HL.text()
        H_right_string = self.lineEdit_HR.text()
        
        F_IK_con = self.lineEdit_H12.text()
        F_pole_con = self.lineEdit_H11.text()
        F_FK_shoulder = self.lineEdit_H13.text()
        F_FK_wrist = self.lineEdit_H15.text()
        F_left_string = self.lineEdit_FL.text()
        F_right_string = self.lineEdit_FR.text()
        
        self.create_newnode(H_IK_con.replace(H_right_string,H_left_string),\
        H_pole_con.replace(H_right_string,H_left_string),\
        H_FK_shoulder.replace(H_right_string,H_left_string),\
        H_FK_wrist.replace(H_right_string,H_left_string),\
        )#���������������ڵ�
        self.create_newnode(H_IK_con.replace(H_left_string,H_right_string),\
        H_pole_con.replace(H_left_string,H_right_string),\
        H_FK_shoulder.replace(H_left_string,H_right_string),\
        H_FK_wrist.replace(H_left_string,H_right_string),\
        )#���������������ڵ�
        self.create_newnode(F_IK_con.replace(F_left_string,F_right_string),\
        F_pole_con.replace(F_left_string,F_right_string),\
        F_FK_shoulder.replace(F_left_string,F_right_string),\
        F_FK_wrist.replace(F_left_string,F_right_string),\
        )#���ҽ����������ڵ�
        self.create_newnode(F_IK_con.replace(F_right_string,F_left_string),\
        F_pole_con.replace(F_right_string,F_left_string),\
        F_FK_shoulder.replace(F_right_string,F_left_string),\
        F_FK_wrist.replace(F_right_string,F_left_string),\
        )#��������������ڵ�
        return True
    
    def write_into_config(self):#����д������
        config_content = self.readline()#������Ϣ
        config_string = ""#��Ҫд��ȥ���ַ���
        for _ in range(len(config_content)):
            if config_content[_] == "":
                self.erroroutput("����δ��д�������ȫ�����ϲ������ɽű���")
                return
            else:
                config_string += config_content[_]+"%%" #������%�������ַ�
        #print config_string
        if config_string:
            #print config_string 
            apply_ornot = mc.confirmDialog(title=u'��ʾ', message="���ɹ��ߺ�ð󶨽���ÿ���ֱۺ������������任�ڵ�\n������ȷ�������ʲ���������״̬��������", button=['ok','no'])
            if apply_ornot == "ok":    
                script_dir = mc.internalVar(usd=1)
                file_path = script_dir+"DH_IKFKSW/projects"#�����ɵ������ļ����ᱻ����C:\Users\%username%\Documents\maya\2017\scripts\DH_IKFKSW\projects
                if file_path:
                    config_file = open(file_path+'/'+config_content[0]+'.swfile', 'w')
                    config_file.write(config_string.encode('utf-8'))
                    config_file.close()
                self.create_newnode_doit()
        return  config_content[-1]   
    
    
      
DH_IKFKSW_Generator_Tool().show()      

    







